package com.seanliao.nav.security;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * 权限管理器
 */
@Slf4j
@Component("pms")
public class PermissionFilter {

    /**
     * 所有权限标识
     */
    private static final String ALL_PERMISSION = "*:*";

    /**
     * 判断当前用户是否用有指定权限
     *
     * @param permissions
     * @return
     */
    public boolean hasPermission(String... permissions) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            return false;
        }
        MyUserDetails userDetails = (MyUserDetails) authentication.getPrincipal();
        // ALL_PERMISSION 全部权限
        if (userDetails.getPermissions().contains(ALL_PERMISSION)) {
            return true;
        }
        List<String> permissionList = Arrays.asList(permissions);
        if (permissionList.isEmpty()) {
            return false;
        }
        for (String permission : permissionList) {
            if (!userDetails.getPermissions().contains(permission)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 拥有其中任一权限
     */
    public boolean hasAnyPermission(String... permissions) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            return false;
        }
        MyUserDetails userDetails = (MyUserDetails) authentication.getPrincipal();
        // ALL_PERMISSION 全部权限
        if (userDetails.getPermissions().contains(ALL_PERMISSION)) {
            return true;
        }
        List<String> permissionList = Arrays.asList(permissions);
        if (permissionList.isEmpty()) {
            return false;
        }
        for (String permission : permissionList) {
            if (userDetails.getPermissions().contains(permission)) {
                return true;
            }
        }
        return false;
    }


    /**
     * 拥有全部角色
     */
    public boolean hasRoles(String... roles) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            return false;
        }
        MyUserDetails userDetails = (MyUserDetails) authentication.getPrincipal();
        List<String> roleList = Arrays.asList(roles);
        if (roleList.isEmpty()) {
            return false;
        }
        for (String string : roleList) {
            if (!userDetails.getRoles().containsKey(string)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 拥有其中任一角色
     */
    public boolean hasAnyRoles(String... roles) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            return false;
        }
        MyUserDetails userDetails = (MyUserDetails) authentication.getPrincipal();
        List<String> roleList = Arrays.asList(roles);
        if (roleList.isEmpty()) {
            return false;
        }
        for (String role : roleList) {
            if (userDetails.getRoles().containsKey(role)) {
                return true;
            }
        }
        return false;
    }

}
